﻿using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;

namespace BeanInvsee
{
    [ApiVersion(2, 1)]
    public class MainPlugin : TerrariaPlugin
    {
        
        public MainPlugin(Main game) : base(game){}
        public override string Name => "BeanInvsee";

        public override Version Version => Assembly.GetExecutingAssembly().GetName().Version;

        public override string Author => "豆沙";

        public override string Description => "a invsee plugin";

        public override void Initialize()
        {
            Commands.ChatCommands.Add(new Command("invsee.use",InvSee,"invsee"));
        }

        private void InvSee(CommandArgs args)
        {
            if (args.Parameters.Count<1)
            {
                args.Player.SendInfoMessage("输入/invsee [玩家名] 来查看玩家背包");
                return;
            }
            string playerName = args.Parameters[0];
            if (args.Parameters.Count > 1)
            {
                playerName = string.Join(" ", args.Parameters);
            }
            var players =TSPlayer.FindByNameOrID(playerName);
            if (players.Count != 0)
            {
                var tsplayer = players[0];
                StringBuilder total = new StringBuilder();
                string inventory = Utils.GetInventoryString(tsplayer.TPlayer.inventory,NetItem.InventorySlots);
                string armor = Utils.GetInventoryString(tsplayer.TPlayer.armor, NetItem.ArmorSlots);
                string dye = Utils.GetInventoryString(tsplayer.TPlayer.dye, NetItem.DyeSlots);
                string miscEquip = Utils.GetInventoryString(tsplayer.TPlayer.miscEquips, NetItem.MiscEquipSlots);
                string miscDye = Utils.GetInventoryString(tsplayer.TPlayer.miscDyes, NetItem.MiscDyeSlots);
                string trashItem = $"[[i/s{tsplayer.TPlayer.trashItem.stack}:{tsplayer.TPlayer.trashItem.netID}]]";
                total.AppendLine($"玩家[{tsplayer.Name}]背包如下:");
                total.AppendLine("背包:"+inventory);
                total.AppendLine("饰品+时装:" + armor);
                total.AppendLine("染料:" + dye);
                total.AppendLine("杂项:" + miscEquip);
                total.AppendLine("杂项染料:" + miscDye);
                total.AppendLine("垃圾桶:" + trashItem);
                args.Player.SendMessage(total.ToString(),Color.MediumAquamarine);
            }
            else {

                args.Player.SendInfoMessage("所查询玩家不在线，正在查询离线数据");
                string tip = $"玩家[{playerName}]背包如下:";
                string info=Utils.GetOfflinePlayerInv(TShock.DB,playerName);
                args.Player.SendMessage(tip+"\n"+info,Color.MediumAquamarine);
            }

        }

        protected override void Dispose(bool disposing)
        {



        }
    }
}
